package tacocloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;
import tacocloud.dao.Ingredient;
import tacocloud.dao.Ingredient.Type;
import tacocloud.dao.Order;
import tacocloud.dao.Taco;
import tacocloud.data.IngredientRepository;
import tacocloud.data.TacoRepository;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/design")
@SessionAttributes("order")
public class DesignTacoController {

//    获得数据
    private final IngredientRepository ingredientRepo;
    private TacoRepository designRepo;

    @Autowired
    public DesignTacoController(
            IngredientRepository ingredientRepo,
            TacoRepository designRepo) {
        this.ingredientRepo = ingredientRepo;
        this.designRepo = designRepo;
    }


    /**
     * 展示配置菜单
     *
     * @param model
     * @return
     */
    @GetMapping
    public String showDesignForm(Model model){
        /** 将直接载入数据改变成使用findAll()方法载入
         *         List<Ingredient> ingredints= Arrays.asList(
         *                 new Ingredient("FLTO","Flour Torilla", Type.WRAP),
         *                 new Ingredient("COTO","Corn Torilla", Type.WRAP),
         *                 new Ingredient("GRBF","Ground Beef", Type.PROTEIN),
         *                 new Ingredient("CARN","Carnitas", Type.PROTEIN),
         *                 new Ingredient("TMTO","Diced Tomatoes", Type.VEGGIES),
         *                 new Ingredient("LETC","Lettuce", Type.VEGGIES),
         *                 new Ingredient("CHED","Cheddar", Type.CHEESE),
         *                 new Ingredient("JACK","Monterrey", Type.CHEESE),
         *                 new Ingredient("SLSA","Salsa", Type.SAUCE),
         *                 new Ingredient("SRCR","Sour Cream", Type.SAUCE)
         *                 );
         */
        List<Ingredient> ingredints=new ArrayList<>();
        ingredientRepo.findAll().forEach(ingredints::add);

        Type[] types = Ingredient.Type.values();
        for (Type type:types){
            model.addAttribute(type.toString().toLowerCase(),
                    filterByType(ingredints,type));
        }

        model.addAttribute("design",new Taco());

        return "design";
    }

    @ModelAttribute(name="order")
    public Order order(){
        return new Order();
    }

    @ModelAttribute(name="taco")
    public Taco taco(){
        return new Taco();
    }

    @PostMapping
    public String processDesign(
            @Valid Taco design,Errors errors,
            @ModelAttribute Order order){
        if (errors.hasErrors()){
            return "design";
        }

        Taco saved=designRepo.save(design);
        order.addDesign(saved);

        return "redirect:/orders/current";
    }

    private List<Ingredient> filterByType(
            List<Ingredient> ingredients, Type type) {
        return ingredients
                .stream()
                .filter(x->x.getType().equals(type))
                .collect(Collectors.toList());
    }

    /**
     * 保存菜单设计
     * @param design
     * @return
     */
    @PostMapping
    public String processDesign(@Valid Taco design, Errors errors){
//        保存此taco设计
        if (errors.hasErrors()){
            return "design";
        }

        // save the taco design
        log.info("Processing design: "+design);
        return "redirect:/orders/current";
    }
}
